<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <template>
              <div style="width:100%;">
                  <div style="float:left;width:50%">
                      <table style="font-size:12px" class="table1style">
                          <tr>
                              <td>快速查询</td>
                              <td>
                                  <el-input v-model.trim="txtsearch" placeholder="科室代码或名称或拼音码" style="width:200px" clearable @input="quickQuery($event)"></el-input>
                              </td>
                              <td v-show="isRegLis">医院</td>
                              <td v-show="isRegLis">
                                  <rm-ddl-common @change="hospitalChange" datatype="hospital" dataname="医院" v-model="ddlhospcode" width="180px"></rm-ddl-common>
                              </td>
                          </tr>
                      </table>
                  </div>
                  <div style="float:left;width:50%;">
                      <table style="float:right">
                          <tr>
                              <td>
                                  <el-button icon="el-icon-search" @click="RefreshList" v-has="'PINQUERY'" type="primary">查询</el-button>
                              </td>
                              <td>
                                  <el-button @click="add" icon="el-icon-plus" v-has="'PADD'">新增</el-button>
                              </td>
                              <td>
                                  <el-button @click="del" icon="el-icon-delete" v-has="'PDELETE'">删除</el-button>
                              </td>
                              <td>
                                  <el-button @click="save" icon="el-icon-document">保存</el-button>
                              </td>
                              <td>
                                  <el-button @click="recordexport" icon="el-icon-upload2" v-has="'PEXPORT'">导出</el-button>
                              </td>
                              <td>
                                  <el-button @click="syncdata" icon="el-icon-position" v-has="'PSYNC'">同步</el-button>
                              </td>
                              <td>
                                  <el-button @click="dangervalperson" icon="el-icon-close-notification">危急值人员</el-button>
                              </td>
                          </tr>
                      </table>
                  </div>
              </div>
          </template>
      </div>
      <div class="pagecenter">
          <div class="middle-center">
              <vxe-table show-overflow ref="xTable1" class="mytable-style" height="auto" :loading="loading" :select-config="{reserve: true}" :sort-config="{trigger: 'cell'}" :keyboard-config="keyConfig" :mouse-config="{selected: true}" :data.sync="tableDataShow" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable :edit-config="{trigger: 'click', mode: 'cell',showStatus: true,showIcon:false,activeMethod:cellactiveMethod}" :cell-class-name="cellClassName" @edit-closed="EditClose" :row-class-name="rowClassName" :edit-rules="dept_validRules" @current-change="currentChangeEvent" @cell-dblclick="celldblclick">
                  <vxe-table-column type="index" title="NO." width="50" header-align="center" align="center"></vxe-table-column>
                  <vxe-table-column field="deptno" title="科室代码" width="70" sortable header-align="center" :edit-render="{name:'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: blue;">科室代码</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="deptname" title="科室名称" width="120" sortable header-align="center" :edit-render="{name:'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: blue;">科室名称</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="his_deptno" title="科室代码(HIS)" width="90" header-align="center" :edit-render="{name:'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: blue;">科室代码(HIS)</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="deptenname" title="英文名称" width="100" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="telnum" title="联系电话" width="98" sortable header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="depttypename" title="科室类别(双击选择)" width="100" header-align="center" :edit-render="{name:'input', autoselect: true}">
                  </vxe-table-column>
                  <!-- <vxe-table-column field="deptsubject" title="专业类别" width="90" header-align="center" :edit-render="{name:'select', options: DeptSubjectOptions}">
                  </vxe-table-column> -->
                  <vxe-table-column field="deptsubjectname" title="专业类别(双击开窗)" width="100" header-align="center" :edit-render="{name:'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="dangercnt" title="危急值通知人数" width="100" header-align="center" align="center"></vxe-table-column>
                  <vxe-table-column field="hospcode" title="医院代码" width="120" header-align="center" :edit-render="{name:'select', options: HospitalOptions}">
                  </vxe-table-column>
                  <vxe-table-column field="description" title="备注说明" width="120" header-align="center" :edit-render="{name:'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="invalidflag" key="invalidflag" title="作废标志" width="60" header-align="center" align="center">
                      <template v-slot="{row}">
                          <el-checkbox v-model="row.invalidflag" true-label="1" false-label="0"></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="inputcode" title="输入码" width="100" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="isperformby" key="isperformby" title="检验科执行科室" width="100" header-align="center" align="center">
                      <template v-slot="{row}">
                          <el-checkbox v-model="row.isperformby" true-label="1" false-label="0"></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="parentdeptno" title="上级科室代码(双击开窗)" width="150" header-align="center">
                      <template v-slot="{row}">
                          <el-input v-model="row.parentdeptnoname" @dblclick.native="AlertParentdeptno" readonly></el-input>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="whocode" title="whonet(专业类别DEPARTMENT)" width="210" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="whokind" title="whonet(科室类别WARD_TYPE)" width="210" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="externalcode1" title="外部代码1" width="90" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="externalcode2" title="外部代码2" width="90" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="externalcode3" title="外部代码3" width="90" header-align="center" :edit-render="{name:'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="bedsnum" title="床位数" width="80" header-align="center" :edit-render="{name:'input', autoselect: true}" align="center"></vxe-table-column>
              </vxe-table>
          </div>
      </div>
      <el-dialog title="提示" :visible.sync="dialogDelVisible" width="30%">
          <span class="el-icon-info">确定删除科室名称为<b style="color:red">{{message}}</b>的记录吗？</span>
          <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="confirmdel">确 定</el-button>
              <el-button @click="dialogDelVisible=false" :autofocus="true">取 消</el-button>
          </span>
      </el-dialog>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择上级科室" :visible.sync="dialogdeptVisible">
          <rm-pop-common @change="deptchange" @cancel="dialogdeptVisible=false;" datatype="dept"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="请选择科室对应危急值通知人员" :visible.sync="dialogDangervalVisible" width="1075px">
          <template>
              <div style="height:500px;width:100%;position:relative;">
                  <div class="poppagetop">
                      <div style="float:left;width:50%;text-align:center;margin-top:5px">
                          <table class="table1style">
                              <tr>
                                  <td>快速查询</td>
                                  <td>
                                      <el-input v-model="txtquicksearch" style="width:400px" placeholder="医生代码或医生名称或输入码" @keyup.enter.native="doctorquery"></el-input>
                                  </td>
                              </tr>
                          </table>
                      </div>
                      <div style="float:left;width:50%;text-align:center;margin-top:5px">
                          <el-button @click="dialogDangervalVisibleSave">保 存</el-button>
                          <el-button @click="dialogDangervalVisible = false">取 消</el-button>
                      </div>
                  </div>
                  <div class="poppagecenter">
                      <div class="popmiddle-left">
                          <vxe-table show-overflow ref="xTable2" height="auto" :loading="loading" @cell-dblclick="selectchange" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" @keyup.enter.native="popenterselect" fit :data.sync="tableData2" show-header-overflow highlight-current-row highlight-hover-row resizable border size="mini" @current-change="popcurrentChangeEvent">
                              <vxe-table-column type="index" title="NO." width="50" align="center"></vxe-table-column>
                              <vxe-table-column field="doctorno" title="医生代码" width="80" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="doctorname" title="医生姓名" width="80" header-align="center" align="center"></vxe-table-column>
                              <vxe-table-column field="doctorkind" title="医生类别" width="80" header-align="center" align="center">
                              </vxe-table-column>
                              <vxe-table-column field="inputcode" title="输入码" width="80" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="shortmsgcode" title="短信接收号码" width="100" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="his_doctorno" title="HIS代码" width="100" header-align="center">
                              </vxe-table-column>
                          </vxe-table>
                      </div>
                      <div class="popmiddle-center">
                          <div style="margin-top:150px">
                              <table>
                                  <tr>
                                      <td>
                                          <el-button type="button" style="width:60px;height:30px;" @click.native="popinsertdata"><span><i class="el-icon-right" style="font-size:17px"></i></span></el-button>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <el-button type="button" style="width:60px;height:30px;" @click.native="popdeletedata"><span><i class="el-icon-back"></i></span></el-button>
                                      </td>
                                  </tr>
                              </table>
                          </div>
                      </div>
                      <div class="popmiddle-right">
                          <vxe-table show-overflow ref="xTable3" height="auto" :loading="loading" @cell-dblclick="delselectchange" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" fit :data.sync="tableData3" show-header-overflow highlight-current-row highlight-hover-row resizable border size="mini" @current-change="popcurrentChangeEvent2">
                              <vxe-table-column type="index" title="NO." width="50" align="center"></vxe-table-column>
                              <vxe-table-column field="userno" title="医生代码" width="80" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="doctorname" title="医生姓名" width="80" header-align="center" align="center"></vxe-table-column>
                              <vxe-table-column field="shortmsgcode" title="短信接收号码" width="100" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="deptwardno" title="科室代码" width="100" header-align="center">
                              </vxe-table-column>
                              <vxe-table-column field="vstype" title="对照类别" width="80" header-align="center">
                              </vxe-table-column>
                          </vxe-table>
                      </div>
                  </div>
              </div>
          </template>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择科室类别" :visible.sync="dialogdepttype">
          <rm-pop-common ref="depttype" @change="depttypeChange" @cancel="dialogdepttype=false;" :multiple="false" kindno="depttype" datatype="seccode"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择专业类别" :visible.sync="dialogdeptsubject">
          <rm-pop-common ref="deptsubject" @change="deptsubjectChange" @cancel="dialogdeptsubject=false;" :multiple="false" kindno="deptsubject" datatype="seccode"></rm-pop-common>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
import XLSX from "xlsx";
import FileSaver from "file-saver";
export default {
  name: "MNG210",
  data() {
      return {
          txtsearch: "",
          loading: false,
          okFirstLoading: true,
          isFirstLoading: false,
          dialogDelVisible: false,
          dialogdeptVisible: false,
          dialogDangervalVisible: false,
          tempindex: 0,
          actionindex: "Q",
          selectrow: [],
          popselectrow: [],
          popdelselectrow: [],
          message: "",
          txtquicksearch: "",
          utype: this.$tool.getSession(this.$tool.loginutype),
          DeptSubjectOptions: [],
          HospitalOptions: [],
          DeptTypeOptions: [
              { value: "", label: "" },
              { value: "01", label: "临床科室" },
              { value: "02", label: "护理单元" },
              { value: "03", label: "医技科室" },
              { value: "04", label: "手术室" },
              { value: "99", label: "其他" },
          ],
          deptno: "",
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
              isArrow: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          selectConfig: {
              tigger: "row",
          },
          isRegLis: false,
          tableData1: [],
          tableData2: [],
          tableData3: [],
          tableDataShow: [],
          ddlhospcode: "",
          dept_validRules: {
              deptno: [{ required: true, message: "科室代码必须填写" }],
              deptname: [{ required: true, message: "科室名称必须填写" }],
              his_deptno: [{ required: true, message: "科室代码(HIS)必须填写" }],
          },
          dialogdepttype: false,
          dialogdeptsubject: false,
          curSelectRow: {},
          // textvalueshow: false,
          // iconclass: "el-icon-arrow-down",
          // editflag: true,
      };
  },
  watch: {
      txtsearch(val, oldval) {
          if (val != oldval) {
              this.$nextTick(() => {
                  var row = this.$refs.xTable1.getCurrentRow();
                  if (this.$refs.xTable1.tableData.length > 0) row = this.$refs.xTable1.tableData[0];
                  if (row.deptno) {
                      this.$refs.xTable1.setCurrentRow(row);
                      this.tempindex = this.$refs.xTable1.getRowIndex(row);
                      this.message = row.deptname;
                  } else {
                      this.message = "";
                  }
              });
          }
      },
      // textvalueshow(val) {
      //     if (val) {
      //         this.iconclass = "el-icon-arrow-up";
      //     } else {
      //         this.iconclass = "el-icon-arrow-down";
      //     }
      // },
  },
  computed: {
      list() {
          let querystr = this.txtsearch.trim();
          if (querystr) {
              let rest = this.tableData1.filter((item) => item.deptno.indexOf(querystr) > -1 || item.deptname.indexOf(querystr) > -1 || (item.inputcode ? item.inputcode.toUpperCase().indexOf(querystr.toUpperCase()) > -1 : false));
              return rest;
          } else {
              return this.tableData1;
          }
      },
  },
  created() {
      this.initialList(); //初始化主列表
      this.ddlInitdata();
      this.$ddldata.IsAreaModeFun().then((r) => {
          this.isRegLis = r;
      }); //防止刷新页面导致IsAreaMode被清掉
  },
  mounted() {},
  updated() {
      if (this.okFirstLoading) {
          this.FunCallUpdateRow();
      }
      if (this.isFirstLoading) {
          let xtable2row = this.$refs.xTable2.getData(0);
          let xtable3row = this.$refs.xTable3.getData(0);
          if (xtable2row) {
              this.isFirstLoading = false;
              this.$refs.xTable2.setCurrentRow(xtable2row);
              this.popselectrow = xtable2row;
          }
          if (xtable3row) {
              this.isFirstLoading = false;
              this.$refs.xTable3.setCurrentRow(xtable3row);
              this.popdelselectrow = xtable3row;
          }
      }
  },
  methods: {
      handleIconClick() {
          this.textvalueshow = true;
          this.editflag = false;
      },

       quickQuery(e) {
         let querystr = this.txtsearch.trim();
          if (querystr) {
              this.tableDataShow = this.tableData1.filter((item) => item.deptno.indexOf(querystr) > -1 || item.deptname.indexOf(querystr) > -1 || (item.inputcode ? item.inputcode.toUpperCase().indexOf(querystr.toUpperCase()) > -1 : false));
          } else {
             this.tableDataShow = this.tableData1;
          }
      },

      RefreshList() {
          this.actionindex = "Q";
          this.initialList();
      },
      ddlInitdata() {
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG210",
                  method: "post",
                  data: {
                      action: "ddlInitialList",
                  },
              })
              .then((response) => {
                  this.DeptSubjectOptions = response.dt1;
                  this.HospitalOptions = response.dt2;
                  this.parentdeptOption = response.dt3;
              });
      },
      FunCallUpdateRow() {
          let currentrow = "";
          if (this.actionindex != "D") {
              if (this.actionindex == "A") {
                  if (this.selectrow != "") {
                      this.$nextTick(() => {
                          var alld = this.$refs.xTable1.getTableData().fullData;
                          for (var i = 0; i < alld.length; i++) {
                              if (alld[i].deptno == this.selectrow.deptno) {
                                  this.tempindex = i;
                                  break;
                              }
                          }
                          currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
                      });
                  } else {
                      currentrow = this.$refs.xTable1.getTableData().fullData[0];
                  }
              } else if (this.actionindex == "M") {
                  this.$nextTick(() => {
                      var alld = this.$refs.xTable1.getTableData().fullData;
                      for (var i = 0; i < alld.length; i++) {
                          if (alld[i].deptno == this.selectrow.deptno) {
                              this.tempindex = i;
                              break;
                          }
                      }
                      currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
                  });
              } else {
                  currentrow = this.$refs.xTable1.getTableData().fullData[0];
              }
          } else {
              if (this.tempindex > 0) {
                  currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
              } else {
                  currentrow = this.$refs.xTable1.getTableData().fullData[0];
              }
          }
          this.$nextTick(() => {
              if (currentrow) {
                  this.okFirstLoading = false;
                  this.message = currentrow.deptname;
                  this.deptno = currentrow.deptno;
                  this.selectrow = currentrow;
              }
          });
          setTimeout(() => {
              this.$refs.xTable1.setCurrentRow(currentrow);
              this.$refs.xTable1.scrollToRow(currentrow);
          }, 100);
      },
      rowClassName({ row, rowIndex }) {
          if (row.hospcode == "All" && this.utype != "G") {
              return "row-gray";
          }
          let isDirty = this.$refs.xTable1.hasRowChange(row);
          if (isDirty) {
              return "row-modify";
          } else {
              if (row.invalidflag == "1") {
                  return "rm-alert";
              }
          }
          if (this.$refs.xTable1.hasRowInsert(row)) return "row-new";
      },
      cellClassName({ columnIndex, column }) {
          if (columnIndex == "0" || column.property == "dangercnt") {
              return "col-gray";
          } else {
              return "";
          }
      },
      cellactiveMethod({ row, column }) {
          if (this.utype == "G") {
              return true;
          } else {
              if (this.$ddldata.IsAreaMode) {
                  if (row.hospcode == "All") {
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  return true;
              }
          }
      },
      initialList() {
          this.loading = true;
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG210",
                  method: "post",
                  data: {
                      action: "query",
                      ddlhospcode: this.ddlhospcode,
                  },
              })
              .then((response) => {
                  response.Data.forEach((item) => {
                      if (item.isperformby != 1) {
                          item.isperformby = "0";
                      }
                  });
                  this.tableData1 = response.Data;
                  this.tableDataShow = response.Data;
                  this.quickQuery();
                  //去除刷新到最底下
                  // this.tableData1.sort((a, b) => {
                  //     return a.invalidflag - b.invalidflag;
                  // });
                  this.loading = false;
                  this.okFirstLoading = true;
              })
              .catch((e) => {
                  console.log("error", e);
                  this.loading = false;
              });
      }, //主页面行选中
      currentChangeEvent({ row, rowIndex }) {
          this.message = row.deptname;
          this.selectrow = row;
          this.deptno = row.deptno;
          this.tempindex = rowIndex;
          this.actionindex = "M"; //修改
          this.okFirstLoading = true;
      },
      recordexport() {
          let data = this.getExportData();
          let book = XLSX.utils.book_new();
          let sheet = XLSX.utils.json_to_sheet(data, { skipHeader: true });
          XLSX.utils.book_append_sheet(book, sheet);
          let wbout = XLSX.write(book, { bookType: "xlsx", bookSST: false, type: "binary" });
          let blob = new Blob([this.toBuffer(wbout)], { type: "application/octet-stream" });
          // 保存导出
          FileSaver.saveAs(blob, "科室字典.xlsx");
          //this.$refs.xTable1.exportCsv({ filename: "科室字典" });
          // var tableColum = this.$refs.xTable1.getTableColumn().fullColumn;
          // tableColum.forEach(element => {
          //     element.field = element.property;
          // });
          // this.$pub.exportTableExcel(this.list, tableColum, "科室字典.xlsx");
      },
      getExportData(index) {
          let datas = this.$refs.xTable1.getTableData().fullData;
          let columns = this.$pub.InitColumn(this.$refs.xTable1.getTableColumn().fullColumn);
          console.log(datas);
          let headers = [columns.map((item) => item.title)];
          return headers.concat(
              datas.map((row) => {
                  return columns.map((column) => {
                      if (column.property == "depttype") {
                          let data1 = this.DeptTypeOptions.filter((m) => m.value == row[column.property]);
                          if (data1.length > 0) {
                              return data1[0].label;
                          }
                      } else if (column.property == "deptsubject") {
                          let data2 = this.DeptSubjectOptions.filter((m) => m.value == row[column.property]);
                          if (data2.length > 0) {
                              return data2[0].label;
                          }
                      } else if (column.property == "hospcode") {
                          let data3 = this.HospitalOptions.filter((m) => m.value == row[column.property]);
                          if (data3.length > 0) {
                              return data3[0].label;
                          }
                      } else if (column.property == "parentdeptno") {
                          let data4 = this.parentdeptOption.filter((m) => m.value == row[column.property]);
                          if (data4.length > 0) {
                              return data4[0].label;
                          }
                      } else {
                          return row[column.property];
                      }
                  });
              })
          );
      },
      toBuffer(wbout) {
          let buf = new ArrayBuffer(wbout.length);
          let view = new Uint8Array(buf);
          for (let index = 0; index !== wbout.length; ++index) view[index] = wbout.charCodeAt(index) & 0xff;
          return buf;
      },
      FunScrollY() {
          let alldataLen = this.$refs.xTable1.getTableData().fullData.length;
          let L = this.$refs.xTable1.getTableData().fullData[alldataLen - 1];
          this.$nextTick(() => {
              this.$refs.xTable1.setCurrentRow(L);
              this.$refs.xTable1.setActiveRow(L, "deptno");
          });
          setTimeout(() => {
              this.$refs.xTable1.scrollToRow(L);
          }, 800);
      },
      add() {
          let initialdata = {
              deptno: "",
              deptname: "",
              telnum: "",
              depttype: "",
              deptsubject: "",
              his_deptno: "",
              dangercnt: "",
              hospcode: this.$ddldata.IsAreaMode ? this.$tool.getSession(this.$tool.selectedhis) : "All",
              description: "",
              invalidflag: "",
              inputcode: "",
              isperformby: "",
              parentdeptno: "",
              whocode: "",
              whokind: "",
              externalcode1: "",
              externalcode2: "",
              externalcode3: "",
              bedsnum: "",
          };
          this.$refs.xTable1.insert(initialdata);
          this.FunScrollY();
          this.actionindex = "A";
          this.message = "";
      },
      del() {
          if (this.utype != "G" && this.selectrow.hospcode == "All") {
              this.$alert("您不是管理员，不能删除医院代码为All的数据！", "提示", {
                  confirmButtonText: "确定",
              });
              return false;
          } else {
              if (this.message == "") {
                  this.confirmdel();
              } else {
                  this.dialogDelVisible = true;
              }
          }
      },
      confirmdel() {
          this.dialogDelVisible = false;
          try {
              var delrow = this.$refs.xTable1.getCurrentRow();
              if (delrow) {
                  var alld = this.$refs.xTable1.getTableData().fullData; //this.$refs.xTable1.tableData;//
                  var rowindex = alld.indexOf(delrow);
                  this.$refs.xTable1.remove(delrow);
                  this.actionindex = "D"; //删除
                  let havedata = this.$refs.xTable1.getTableData().fullData;
                  if (havedata.length > 0) {
                      if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                      let selectrow = havedata[rowindex];
                      this.$refs.xTable1.setCurrentRow(selectrow);
                      this.$refs.xTable1.scrollToRow(selectrow);
                      this.message = selectrow.deptname;
                  } else {
                      this.message = "";
                  }
              }
          } catch (e) {
              alert(e);
          }
      },
      save() {
          let insertresult = [];
          let xTable1date = this.$refs.xTable1.getRecordset();
          let insertobj = xTable1date.insertRecords;
          for (var i = 0; i < insertobj.length; i++) {
              var obj = insertobj[i];
              if (!!obj["deptno"] && !!obj["deptname"] && !!obj["his_deptno"]) {
                  //三个必填值都有值
                  insertresult.push(obj);
              } else {
                  if (!!obj["deptno"] || !!obj["deptname"] || !!obj["his_deptno"]) {
                      //三个必填值其中一个有值
                      let msg = "科室代码(HIS)";
                      if (!obj["deptno"]) {
                          msg = "科室代码";
                      } else if (!obj["deptname"]) {
                          msg = "科室名称";
                      }
                      this.$message.warning(msg + "不能为空");
                  } else if (!obj["deptno"] && !obj["deptname"]) {
                      this.$refs.xTable1.remove(obj);
                  }
                  break;
              }
          }
          xTable1date.insertRecords = insertresult;
          this.$refs.xTable1.validate((valid) => {
              if (valid) {
                  //  let xTable1date = this.$refs.xTable1.getRecordset();
                  if (this.actionindex == "A") {
                      if (xTable1date.insertRecords.length > 0) {
                          this.selectrow = xTable1date.insertRecords[0];
                      }
                  } else if (this.actionindex == "M") {
                      if (xTable1date.updateRecords.length > 0) {
                          this.selectrow = xTable1date.updateRecords[0];
                      }
                  }

                  this.$tool
                      .request({
                          url: "MNG/MNG2/MNG210",
                          method: "post",
                          data: {
                              action: "save",
                              urec: xTable1date,
                          },
                      })
                      .then((response) => {
                          if (response.errcode == "1") {
                              this.$message({
                                  message: "操作成功！",
                                  type: "success",
                              });
                              this.initialList();
                          } else {
                              this.$message({
                                  message: "保存失败！" + response.errmsg,
                                  type: "warning",
                              });
                          }
                      })
                      .catch((e) => {
                          console.log("error", e);
                      });
              }
          });
      },
      editchange() {},
      deptsubjectchange(code, row) {
          //专业类别
          row.deptsubject = code;
      },
      depttypechange(code, row) {
          //科室类别
          row.depttype = code;
      },
      hospitalchange(code, row) {
          //所属医院
          row.hospcode = code;
      },
      deptchange(code, name) {
          //上级科室
          this.selectrow.parentdeptno = code;
          this.selectrow.parentdeptnoname = name;
          this.$refs.xTable1.updateListenter();
          this.dialogdeptVisible = false;
      },
      syncdata() {
          this.loading = true;
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG210",
                  method: "post",
                  timeout: 300000,
                  data: {
                      action: "syncdata",
                  },
              })
              .then((response) => {
                  if (response.err == 0) {
                      let msg = response.msg;
                      if (!msg) msg = "同步数据获取失败！";
                      this.loading = false;
                      this.$alert(msg);
                      return;
                  }
                  let data = response.data;
                  //  console.log("🐘🐘🐘🐘🐘 —— data", data);
                  data.forEach((item) => {
                      let rest = this.tableData1.filter((element) => element.deptno.toUpperCase() == item.deptno.toUpperCase());
                      if (rest.length > 0) {
                          try {
                              rest.forEach((ele) => {
                                  if (ele.deptno.toUpperCase() == item.deptno.toUpperCase()) {
                                      ele.deptname = item.deptname;
                                      ele.deptenname = item.deptenname;
                                      ele.depttype = item.depttype;
                                      ele.his_deptno = item.his_deptno == "" ? ele.deptno : item.his_deptno;
                                      ele.hospcode = item.hospcode == "" || item.hospcode == null ? ele.hospcode : item.hospcode;
                                      ele.deptsubject = item.deptsubject == "" ? ele.deptsubject : item.deptsubject;
                                      ele.telnum = item.telnum == "" ? ele.telnum : item.telnum;
                                      ele.parentdeptno = item.parentdeptno == "" ? ele.parentdeptno : item.parentdeptno;
                                      ele.bedsnum = item.bedsnum == "" ? ele.bedsnum : item.bedsnum;
                                      ele.description = ele.deptname == item.deptname ? null : ele.deptname;
                                      ele.inputcode = item.inputcode;

                                      throw Error();
                                  }
                              });
                          } catch (e) {}
                      } else {
                          let initialdata = {
                              deptno: item.deptno,
                              deptname: item.deptname,
                              deptenname: item.deptenname,
                              his_deptno: item.his_deptno,
                              hospcode: item.hospcode == "" ? "All" : item.hospcode,
                              deptsubject: item.deptsubject,
                              telnum: item.telnum,
                              parentdeptno: item.parentdeptno,
                              depttype: item.depttype,
                              bedsnum: item.bedsnum,
                              invalidflag: item.invalidflag,
                              inputcode: item.inputcode,
                          };
                          this.$refs.xTable1.insert(initialdata);
                      }
                  });
                  this.loading = false;
                  setTimeout(() => {
                      this.$message({
                          type: "warining",
                          message: "科室字典数据更新完成！",
                      });
                  }, 500);
              })
              .catch((e) => {
                  console.log("error", e);
                  this.loading = false;
              });
      },
      celldblclick({ row, column }) {
          if (column.property == "dangercnt") {
              this.dangervalperson();
          } else if (column.property == "depttypename") {
              this.curSelectRow = row;
              this.dialogdepttype = true;
          } else if (column.property == "deptsubjectname") {
              this.curSelectRow = row;
              this.dialogdeptsubject = true;
          }
      },
      dangervalperson() {
          this.doctorquery();
          this.dialogDangervalVisible = true;
      },
      doctorquery() {
          this.docloading = true;
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG210",
                  method: "post",
                  data: {
                      action: "doctorquery",
                      qcode: this.txtquicksearch,
                      deptno: this.deptno,
                  },
              })
              .then((response) => {
                  this.tableData2 = response.RData.Data;
                  this.tableData3 = response.RData2.Data;
                  this.docloading = false;
                  this.isFirstLoading = true;
              })
              .catch((e) => {
                  console.log("error", e);
                  this.docloading = false;
              });
      },
      selectchange({ row }) {
          //双击选中
          // this.popselectrow=row;
          this.popinsertdata();
      },
      delselectchange() {
          //双击从右边删除
          this.popdeletedata();
      },
      popenterselect() {
          //回车选中
          this.popinsertdata();
      },
      popinsertdata() {
          let alldata = this.$refs.xTable3.tableData;
          let flag = false;
          if (alldata.length > 0) {
              alldata.forEach((item) => {
                  if (item.userno == this.popselectrow.doctorno) {
                      flag = true;
                  }
              });
          }
          if (flag == false) {
              let initialdata = {
                  userno: this.popselectrow.doctorno,
                  doctorname: this.popselectrow.doctorname,
                  shortmsgcode: this.popselectrow.shortmsgcode,
                  deptwardno: this.deptno,
                  vstype: "1",
              };
              this.$refs.xTable3.insert(initialdata);
          }
      },
      popdeletedata() {
          var delrow = this.$refs.xTable3.getCurrentRow();
          if (delrow) {
              this.$refs.xTable3.remove(delrow);
              let havedata = this.$refs.xTable3.getTableData().fullData;
              if (havedata.length > 0) {
                  let selectrow = this.$refs.xTable3.getTableData().fullData[havedata.length - 1];
                  this.$refs.xTable3.setCurrentRow(selectrow);
              }
          } else {
              this.$message({
                  message: "请先选中要删除的明细！",
                  type: "warning",
              });
          }
      },
      AlertParentdeptno() {
          this.dialogdeptVisible = true;
      },
      popcurrentChangeEvent({ row }) {
          this.popselectrow = row;
      },
      popcurrentChangeEvent2({ row }) {
          this.popdelselectrow = row;
      },
      EditClose({ row, column, rowIndex }) {
          //写法错误，且代码无意义
          // if (column.property == "description") {
          //     this.$refs.xTable1.$refs.tableBody.$children[rowIndex * 3].$el.focus();
          // } else if (column.property == "inputcode") {
          //     this.$refs.xTable1.$refs.tableBody.$children[rowIndex * 3 + 1].$el.focus();
          // } else
          if (column.property == "deptname") {
              let thisinputcode = "";
              let getpinyin = this.$pinyin(row.deptname, {
                  style: this.$pinyin.STYLE_FIRST_LETTER, // 首字母
                  heteronym: true,
              });
              for (var i = 0; i < getpinyin.length; i++) {
                  var curchar = getpinyin[i];
                  if (curchar.length > 1) {
                      thisinputcode += curchar[0];
                  } else {
                      thisinputcode += curchar;
                  }
              }
              row.inputcode = thisinputcode.toUpperCase();
          } else if (column.property == "deptno") {
              if (row.deptno != "") {
                  row.his_deptno = row.deptno;
              }
          } else if (column.property == "depttypename") {
              row.depttype = row.depttypename;
          } else if (column.property == "deptsubjectname") {
              row.deptsubject = row.deptsubjectname;
          }
      },
      dialogDangervalVisibleSave() {
          let xTable3date = this.$refs.xTable3.getRecordset(); //弹窗保存
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG210",
                  method: "post",
                  data: {
                      action: "dangervalpopsave",
                      urec: xTable3date,
                  },
              })
              .then((response) => {
                  if (response.errcode == "1") {
                      this.$message({
                          message: "操作成功！",
                          type: "success",
                      });
                      this.initialList();
                      this.dialogDangervalVisible = false;
                  } else {
                      this.$message({
                          message: "保存失败！" + response.errmsg,
                          type: "warning",
                      });
                  }
              })
              .catch((e) => {
                  console.log("error", e);
              });
      },
      hospitalChange(code, name) {
          this.ddlhospcode = code;
          this.initialList();
      },
      depttypeChange(code, name) {
          this.curSelectRow.depttype = code;
          this.curSelectRow.depttypename = name;
          this.dialogdepttype = false;
      },
      deptsubjectChange(code, name) {
          this.curSelectRow.deptsubject = code;
          this.curSelectRow.deptsubjectname = name;
          this.dialogdeptsubject = false;
      },
  },
};
</Script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  left: 0px;
  right: 0px;
  overflow: auto;
}
.poppagetop {
  width: 100%;
  height: 40px;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  position: absolute;
  border: 1px solid #ccc;
  z-index: 999;
}
.poppagecenter {
  position: absolute;
  top: 50px;
  width: 100%;
  left: 0;
}
.popmiddle-left {
  position: absolute;
  height: 450px;
  width: 480px;
  left: 0px;
  overflow: auto;
}
.popmiddle-center {
  position: absolute;
  -webkit-box-align: center;
  height: 100%;
  left: 485px;
  right: 485px;
}
.popmiddle-right {
  position: absolute;
  top: 0;
  bottom: 0;
  height: 450px;
  width: 480px;
  right: 0px;
}
.table1style tr td {
  padding: 0px 2px 0px 5px;
}
/deep/.middle-center tr td div .el-input__inner {
  border: 1px solid rgba(255, 254, 254, 0) !important;
  background-color: rgba(255, 254, 254, 0) !important;
}
/deep/ .mytable-style.vxe-table .vxe-body--column.col-gray {
  background-color: #f8f8f8;
}
/deep/ .mytable-style.vxe-table .vxe-body--row.row-gray {
  background-color: #f8f8f8;
}
/deep/ tr.vxe-body--row.row-modify {
  color: blue !important;
}
/deep/ tr.vxe-body--row.row-new {
  color: red !important;
}
.reqitem-dialog {
  position: absolute;
  z-index: 10000;
}
</style>